//获取应用实例
const app = getApp();
const netWork_utils = require('../../../http/NetworkUtils');
const constants = require('../../../common/Constants');
const umfDialog = require('../../../utils/UmfDialog');
import * as umfUtils from '../../../utils/UmfUtils';
import * as regexUtils from '../../../utils/RegexUtils';

import { $Toptips } from '../../../components/CompRouter';

Page({
    data: {
        // errorMsg: '',
    },

    onLoad: function (options) {
        this._requestQueryRate();
    },
    
    onSave: function (e) {
        let minRate = e.detail.value.minRateInput;
        let maxRate = e.detail.value.maxRateInput;

        if (this._checkForm(minRate, maxRate)) {
            this._requestSetRate(minRate, maxRate);
        }
    },

    _checkForm: function (minRate, maxRate) {
        let values = [
            [minRate, constants.RegexTypes.RATE_CHECK],
            [maxRate, constants.RegexTypes.RATE_CHECK]
        ];
        let errorMsg = regexUtils.regexTypesWithMsg(values);
        if (errorMsg) {
            $Toptips.error({text: errorMsg});
            // this.setData({
            //     errorMsg: errorMsg
            // });
            return false;
        }

        let min = parseFloat(minRate);
        let max = parseFloat(maxRate);

        if (min < constants.RATE_MIN) {
            $Toptips.error({text: "最低费率不能小于" + constants.RATE_MIN + "%"});
            // this.setData({
            //     errorMsg: "最低费率不能小于" + constants.RATE_MIN + "%"
            // });
            return false;
        }
        if (max > constants.RATE_MAX) {
            $Toptips.error({text: "最高费率不能大于" + constants.RATE_MAX + "%"});
            // this.setData({
            //     errorMsg: "最高费率不能大于" + constants.RATE_MAX + "%"
            // });
            return false;
        }
        if (min > max) {
            $Toptips.error({text: "最低费率不能大于最高费率"});
            // this.setData({
            //     errorMsg: "最低费率不能大于最高费率"
            // });
            return false;
        }

        // this.setData({
        //     errorMsg: ''
        // });
        return true;
    },

    _requestQueryRate: function () {
        netWork_utils.buildRequest(constants.Interfaces.FUNC_QUERY_RATES, {}, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this.setData({
                        minRate: response.lowRate,
                        maxRate: response.highRate
                    });
                    return true;
                }
                return false;
            }
        }).send();
    },

    _requestSetRate(minRate, maxRate) {
        let params = {
            lowRate: minRate,
            highRate: maxRate
        };
        netWork_utils.buildRequest(constants.Interfaces.FUNC_SAVE_RATES, params, {
            doHttpSucess: (response)=> {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    umfDialog.show('配置已生效', ()=> {
                        wx.navigateBack({delta: 1});
                    });
                    return true;
                }
                return false;
            }
        }).send();
    }
});